package com.aaa.hospital.util;

import com.aaa.hospital.pojo.domain.AliPayBean;
import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

@Data
@Configuration
public class Alipay {

    /**
     * 日志对象
     */
    private static final Logger logger = LoggerFactory.getLogger(Alipay.class);

    private final String format = "json";

    /**
     * appId
     * 填写自己的appid
     */
    @Value("2021000119619171")
    private String appId;

    /**
     * 商户私钥
     * 填写自己的商户私钥
     */
    @Value("MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQC/ig26OStoEVw/Cw14lRdp7aOuNCSAOh19ty+3KjUnNrxXKukqaQpPFZRVDhrLejSLO7So5bJkaDeLTOjVxQeCG1QBmKj41ajRnsvJG7/0aggmWXOVp/7r+Fg9wF58J1v74c1iAD75/GqGyZHz+exnXkdAnu3Hu7Kh269AUFVwGHxYgSQt2jd9A9n1uUx4BVl8xzlcPoGl2RsqpOhi9EiiZ9RRPEqdPxU/iUHpRmU4HU1q7G5Y2KbzMgGcE6RojegoEPShLzBeg4jzgu2mzH1spw7qTa1hmA+QyhXmJgwgiTwg3g7JHoulgP0hsoveSiW7Ne0Qb61f3ONvagl/F3A7AgMBAAECggEAXi8iZOnrwDaZJ57ltt+JLdte2zBkqnDpQ4IqgmTM0kWGA07kbKhOG2k/ETvoTRoAcF6hjVO1js34UhU1gLhr4rxRaJ+KpyFY7Aqrpm5wy1zAOStPPM3o+luZ+HN5go9QGeGEvqXkUcSfFTxU+qVwa7veSZZc0hQn2JNP784Q1Rc5jFpSVNMq8b30MF07xBu63B6u5S+VgiurJPA7Vj7VQU6pOLjW709OleD7JYIztJq6EnU5uqpIUq5J7puuEBwvmkPds7QeP2lblu4mA2bYZj8C9D2kYr1dZBII/O/fP9lXv6E0j5SBRs6zZlD7E0XsBV4CJqlozcB6/vXHhh16EQKBgQD82h5V0G1XCqVYCwcaa4vA2owfkLlGhVLV7TwesASyQHblXGHxfl1LPqp6ADJr150k3T4QbEdkoaSykYTtFuPoW4GaQVxFZU+V2AVDTOMJP79cGtUBDISfM/A0V0u/IdDxEFKLGykwzoajypg4f9PhyTHQAkCNv+JU1QARxc+ORwKBgQDB7IVrkMdT6XkKaAH3kiy9QZrdQ3gue3kGhqQgaSbRPujDTTx5TuCI5tVmIhKdMIgtJsgOU74eSxSvC0aIUxeMPlpBm9FQRxqZxJ5W0RhnYId48/oO8d/hykZe01aS1wcJTB3a9fgk8BzS59Jb2d7UHenvKogJLqABrtLW0itEbQKBgQDD87GQpX8m9mwe6CTwF6ptobD4AWtkOIxyl3CBA74P+h9qLocE5P4eAwmQlLh+lNEGsaKYjaFiXlbJwDZ4msWy2797sO12JkZgBNJHTr2f/FQrZ+vs7SuWSWT82y4riGRpfVHK1hi850C2jJodJwsoLoi5ABXRBIS9Y3izTU5FnwKBgQCQJcsrIMw54KNbi+Jib6KX74s5C6zrCFb29eBMQz1abqPAgMoB9hLLUKKxr4QcmfWvclNk6vS4OeEfGIeOUA2B7ttSR4u4W5I3WMLGD9lPBp7M1rY6o6pigOP8GqYQsouFarIKj6mrc1xJnY5tPi73h2DbqcbC5IiFl7kbe9AzpQKBgQCpgl7cuSUxpZ8rxX8oco/kIYu51L329yEkCMPJ1ShWg+2l+QMvOKpVLSZk9BGdIbs9upK+sE6IKqZ07q+TEDwi4S0f8MwYwprhFCjrBmrjQXIWc5RrCQJGTgSakiH0qginJQk3pm+N1O5b+/QC4uZaACpDetpq6Vuckbex3Yefbw==")
    private String privateKey;

    /**
     * 支付宝公钥
     * 填写自己的支付宝公钥
     */
    @Value("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAv4oNujkraBFcPwsNeJUXae2jrjQkgDodfbcvtyo1Jza8VyrpKmkKTxWUVQ4ay3o0izu0qOWyZGg3i0zo1cUHghtUAZio+NWo0Z7LyRu/9GoIJllzlaf+6/hYPcBefCdb++HNYgA++fxqhsmR8/nsZ15HQJ7tx7uyoduvQFBVcBh8WIEkLdo3fQPZ9blMeAVZfMc5XD6BpdkbKqToYvRIomfUUTxKnT8VP4lB6UZlOB1NauxuWNim8zIBnBOkaI3oKBD0oS8wXoOI84Ltpsx9bKcO6k2tYZgPkMoV5iYMIIk8IN4OyR6LpYD9IbKL3koluzXtEG+tX9zjb2oJfxdwOwIDAQAB")
    private String publicKey;

    /**
     * 服务器异步通知页面路径，需要公网能访问到
     * http://localhost:8080/ssm/error_url.html
     */
    @Value("http://localhost:8080/ssm/error_url.html")
    private String notifyUrl;

    /**
     * 服务器同步通知页面路径，填写自己的成功页面路径
     */
    @Value("http://localhost:8080/ssm/error_url.html")
    private String returnUrl;

    /**
     * 签名方式
     */
    @Value("RSA2")
    private String signType;

    /**
     * 字符编码格式
     */
    @Value("utf-8")
    private String charset;

    /**
     * 支付宝网关
     * https://openapi.alipaydev.com/gateway.do
     */
    @Value("https://openapi.alipaydev.com/gateway.do")
    private String gatewayUrl;

    public String pay(AliPayBean aliPayBean) throws AlipayApiException {

        AlipayClient alipayClient = new DefaultAlipayClient(
                gatewayUrl, appId, privateKey, format, charset, publicKey, signType);
        System.out.println(privateKey);

        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(returnUrl);
        alipayRequest.setNotifyUrl(notifyUrl);
        alipayRequest.setBizContent(JSON.toJSONString(aliPayBean));
        logger.info("封装请求支付宝付款参数为:{}", JSON.toJSONString(alipayRequest));

        String result = alipayClient.pageExecute(alipayRequest).getBody();
        logger.info("请求支付宝付款返回参数为:{}", result);

        return result;
    }
}




